﻿using UnityEngine;

namespace Kylin
{
    public class Bootup : MonoBehaviour
    {
        private string managerName = "GameManager";
        private GameObject go = null;

        void Awake()
        {
            if (GameObject.Find(managerName) == null)
            {
                go = base.gameObject;
                go.name = managerName;

                SystemInfoUtils.Instance.RecordSystemInfo();
                InitSettings();
                InitGameMangerObject();
            }
        }

        private void InitGameMangerObject()
        {
            go.AddComponentIfNotExist<Logger>();
            ConfigLogger();

#if !RELEASE_TARGET
            go.AddComponentIfNotExist<FPSDisplay>();
#endif
            go.AddComponentIfNotExist<AtlasLoader>();
            go.AddComponentIfNotExist<SceneLoader>();
            go.AddComponentIfNotExist<LuaManager>();
            go.AddComponentIfNotExist<ResourceManager>();
            go.AddComponentIfNotExist<MonoBeBehaviourManager>();

            go.AddComponentIfNotExist<GameManager>();

            DontDestroyOnLoad(go);            
        }

        private void InitSettings()
        {
            Screen.sleepTimeout = SleepTimeout.NeverSleep;
            Application.runInBackground = true;
            Application.targetFrameRate = ConstDefines.GameFrameRate;

            BundleConfig.Instance().Init();
            GUIDependencyConfig.Instance().Init();
        }

        private void ConfigLogger()
        {
#if RELEASE_TARGET
            Logger.Instance.SetFilter((byte) (Logger.FilterType.Assert | Logger.FilterType.Error | Logger.FilterType.Exception));
#else
            Logger.Instance.SetFilter((byte)Logger.FilterType.All);
#endif
        }
    }
}